<?php

namespace App\Http\Controllers\Home;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\UserInsertRequest;
use App\Http\Controllers\Admin\CateController;

class GoodsController extends Controller
{
	// 商品列表
   	public function getIndex(Request $request)
	{
		$cates = CateController::getCatesByPid(0);
		 //判断是否有查询条件
        if($request->input('keywords')){
            $goods = DB::table('goods')->where('gname','like','%'.$request->input('keywords').'%')->get();
        }else{
            $goods = DB::table('goods')->get();
            // dd($goods);
        }
        $goods = array_chunk($goods, 3);
        $list = $request->all();
         // 查询商品规格表的品牌
         $brand = DB::table('norms')->lists('brand');
        $brand = array_unique($brand);
        // dd($list);
        //解析模板
        return view('home.goods.index',['goods'=>$goods,'list'=>$list,'cates'=>$cates,'brand'=>$brand]);
	}

	// 前台首页菜单
	public function getScreach(Request $request)
	{
		$cates = CateController::getCatesByPid(0);
		// 获取分类名称
		$tname = $request->input('tname');
		// 通过分类名查询分类
		$cate = DB::table('cate')->where('tname',$tname)->get();
		// 获取分类id
		$tid = $cate[0]->tid;
		// 通过tid查询商品
		$goods = DB::table('goods')->where('tid',$tid)->get();
		// dd($goods);
		// 分割数组

		 $goods = array_chunk($goods, 3);
        // dd($goods);
        $list = $request->all();
        // dd($list);
        // 查询商品规格表的品牌
         $brand = DB::table('norms')->lists('brand');
        $brand = array_unique($brand);
        //解析模板
        return view('home.goods.index',['goods'=>$goods,'list'=>$list,'cates'=>$cates,'brand'=>$brand]);
	}

	// 前台首页优惠专场
	public function getNewgoods(Request $request)
	{
		$cates = CateController::getCatesByPid(0);
		// 获取商品状态
		$status = $request->all();
		$status = $status['status'];
		// dd($status);
		// 通过商品状态查找商品
		$goods = DB::table('goods')->where('status',$status)->get();
		// dd($goods);
		// 分割数组
		$goods = array_chunk($goods, 3);
        // dd($goods);
        $list = $request->all();
        // dd($list);
        // 查询规格表获取品牌
        $brand = DB::table('norms')->lists('brand');
        $brand = array_unique($brand);
        // dd($brand);
        //解析模板
        return view('home.goods.list',['goods'=>$goods,'list'=>$list,'cates'=>$cates,'brand'=>$brand]);

	}

	// 前台商品品牌列表
	public function getBrand(Request $request)
	{
		$cates = CateController::getCatesByPid(0);
		// 获取品牌名称
		$brand = $request->all();
		// 通过品牌联查norms和goods两表，筛选出商品
		$goods = DB::table('goods')
            ->join('norms', 'goods.gid', '=', 'norms.gid')->where('brand',$brand)
			->get();
		$goods = array_chunk($goods, 3);	
		// dd($goods);	
		 $list = $request->all();
		  // 查询规格表获取品牌
        $brand = DB::table('norms')->lists('brand');
        $brand = array_unique($brand);	
		// 解析模板	
		 return view('home.goods.list',['goods'=>$goods,'list'=>$list,'cates'=>$cates,'brand'=>$brand]);
		
	}

	

	// 前台商品颜色列表
	public function getColor(Request $request)
	{
		$cates = CateController::getCatesByPid(0);
		// 获取点击商品颜色
		$color = $request->all();
		// dd($brand);
		// 通过颜色联查norms和goods两表，筛选出商品
		$goods = DB::table('goods')
            ->join('norms', 'goods.gid', '=', 'norms.gid')->where('color','like','%'.$color['color'].'%')
			->get();
		// dd($goods);	
		$goods = array_chunk($goods, 3);	
		 $list = $request->all();
		 // dd($list);
		 	  // 查询规格表获取品牌
        $brand = DB::table('norms')->lists('brand');
        $brand = array_unique($brand);	
		// 解析模板	
		 return view('home.goods.lists',['goods'=>$goods,'list'=>$list,'cates'=>$cates,'brand'=>$brand]);
		
	}


	// 前台商品尺寸列表
	public function getSize(Request $request)
	{
		$cates = CateController::getCatesByPid(0);
		// 获取点击商品颜色
		$size = $request->all();
		// dd($brand);
		// 通过颜色联查norms和goods两表，筛选出商品
		$goods = DB::table('goods')
            ->join('norms', 'goods.gid', '=', 'norms.gid')->where('size','like','%'.$size['size'].'%')
			->get();
		// dd($goods);	
		$goods = array_chunk($goods, 3);	
		 $list = $request->all();
		 	  // 查询规格表获取品牌
        $brand = DB::table('norms')->lists('brand');
        $brand = array_unique($brand);	
		 // dd($brand);
		// 解析模板	
		 return view('home.goods.lists',['goods'=>$goods,'list'=>$list,'cates'=>$cates,'brand'=>$brand]);
		
	}

	// 商品详情
	public function getDetails(Request $request)
	{
		// 获取商品id
		$gid = $request->all();
		$gid = $gid['gid'];
		// dd($gid);
		// 菜单传递分类
		$cates = CateController::getCatesByPid(0);
		// 通过gid查询goods和norms表
		$goods = DB::table('goods')
			->join('norms','goods.gid','=','norms.gid')->where('goods.gid','=',$gid)
			->get();
		$tid = $goods[0]->tid;
		$color = $goods[0]->color;
		$color = explode(',',$color);
		$size = $goods[0]->size;
		$size = explode(',',$size);
		// dd($goods);
		// 通过分类查询同类别商品good做商品推荐
		$good = DB::table('goods')->where('tid',$tid)->get();
		// 通过gid查询查询商品评论表comment
		$comment = DB::table('comment')->where('gid',$gid)->get();
		// 通过comment获取id
		// $id = $comment[0]->id;
		// 通过id查询user表
		// $user = DB::table('user')->where('id',$id)->get();
		// 通过user表获取用户名uname
		// $username = $user[0]->username;
		// dd($username);
		return view('home.goods.details',['cates'=>$cates,'goods'=>$goods,'good'=>$good,'color'=>$color,'size'=>$size,'comment'=>$comment]);
	}



	public function getCollectadd(Request $request)
	{
		// 获取传递过来的id和gid
		$data = $request->all();
		 // dd($data);
		// 获取用户id查询用户收藏商品
		$data['id'] = session('uid');
		//$id = session('uid');
		// dd($data);
		$gid = $data['gid'];
		// 获取已登录用户的收藏商品gid
		$gids = DB::table('collect')->where('id',$data['id'])->lists('gid');
		// dd($gids);
		// 判断商品是否已收藏
		if(in_array($gid,$gids)){
			echo 0;
		}else{
			$res = DB::table('collect')->insert($data);
			// dd($res);
			echo $res;
		}
		
	}

	public function getCollect(Request $request)
	{
		// $id = $request->all();
		$id = session('uid');
		// 通过表cllect中的id条件联查cllect表和goods表
		// 获取用户收藏的商品$goods
		$goods = DB::table('collect')
					->join('goods','collect.gid','=','goods.gid')
					->where('collect.id',$id)
					->get();
		$goods = array_chunk($goods, 4);			
		// dd($goods);
		$cates = CateController::getCatesByPid(0);
		return view('home.goods.collect',['cates'=>$cates,'goods'=>$goods]);
	}

	public function getCollectdel(Request $request)
	{
		$data = $request->all();
		// dd($data);
		// 获取用户id查询用户收藏商品
		$id = session('uid');
		$gid = $data['gid'];
		// dd($id);
		$res = DB::table('collect')->where(['id'=>$id,'gid'=>$gid])->delete();
		// dd($res);
		echo $res;
	
	}
	

}
